package com.h3c.nem.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonValue;
import lombok.AllArgsConstructor;
import lombok.Getter;

import java.util.Map;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;

/**
 * @author chenyifan
 * @date 2022/7/20 16:06
 */
@Getter
@AllArgsConstructor
public enum OfferWorkerOfferStatusEnum {

    /**
     * 未发offer
     */
    NO_SEND_OFFER(0, "未发offer"),
    /**
     * 已发offer
     */
    SEND_OFFER(1, "已发offer"),
    /**
     * 接受offer
     */
    ACCEPT_OFFER(2, "接受offer"),
    /**
     * 拒绝offer
     */
    REFUSE_OFFER(3, "拒绝offer");


    @EnumValue
    private final Integer code;

    @JsonValue
    private final String statusName;

    /**
     * code 作为 key，封装为 Map
     */
    private static final Map<Integer, OfferWorkerOfferStatusEnum> CODE_MAP = Stream
            .of(OfferWorkerOfferStatusEnum.values())
            .collect(Collectors.toMap(OfferWorkerOfferStatusEnum::getCode, Function.identity()));


    /**
     * 静态工厂反序列化
     *
     * @param key code
     * @return 评论的类型枚举
     */
    @JsonCreator(mode = JsonCreator.Mode.DELEGATING)
    public static OfferWorkerOfferStatusEnum valueOfKey(Integer key) {
        return Optional.ofNullable(CODE_MAP.get(key))
                .orElseThrow(() -> new IllegalArgumentException(String.valueOf(key)));
    }
}
